package com.hzx.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hzx.entity.Dept;
import com.hzx.service.DeptService;

@Controller
@Scope("prototype")
public class DeptAction extends BaseAction<Dept> {
	@Autowired
	private DeptService deptService;

	private String findDname;

	public void setFindDname(String findDname) {
		this.findDname = findDname;
	}
	public String add(){
		deptService.save(model);
		return LIST;
	}
	// 修改 添加页面
	public String saveOrupdate() throws IOException {
		// 清除session
		session.remove("dept");
		deptService.saveOrupdate(model);
		return NONE;
	}

	public String getDeptId() {
		Dept dept = deptService.findById(model.getDid());
		session.put("dept", dept);
		return UPDATE;
	}

	// 检查部门是否存在
	public String checkDept() {
		Dept dept = deptService.checkDept(model.getDname());
		if (dept != null) {
			// 向前端发送消息 该部门存在
			writeJsp("exist");
		}
		return NONE;
	}

	// 撤销部门
	public String deleteDept() throws IOException {
		deptService.deleteDept(model.getDid());
		writeJsp("success");
		objToJson(pageBean);
		return NONE;
	}

	// 显示未被撤销的部门 分页
	public String findByPage() throws IOException {
		DetachedCriteria criteria = pageBean.getCriteria();
		// 添加条件
		criteria.add(Restrictions.eq("dis", model.getDis()));
		deptService.pageQuery(pageBean);
		// java转json
		objToJson(pageBean, new String[] { "emps" });
		return NONE;
	}

	// 恢复部门
	public String restoreDept() throws IOException {
		deptService.restoreDept(model.getDid());
		writeJsp("success");
		//objToJson(pageBean,new String[]{"emps"});
		return NONE;
	}
	// 没有被撤销的部门
	public String findNoAllDept() throws IOException {
		List<Dept> list = deptService.findNoAllDept();
		listToJson(list, new String[] { "emps", "dremark" });
		return NONE;
	}

}
